app.controller('BusVehicleListCtrl', function($scope, ajax, $modal, toaster) {

    var inserter = new Req.POST({ resource: $scope.resource });
    var deleter = new Req.DELETE({ resource: $scope.resource });
    var updater = new Req.PUT({ resource: $scope.resource });

    $scope.reload = $scope.page.loader('vehicle/bs/{pageIndex}/{pageSize}/page');
    $scope.$on('pageIndex', function(event, p) { $scope.reload(p, $scope.queryMap); });
    $scope.reload(1);

    $scope.queryMap = {};
    $scope.query = function() {
        if ($scope.orglist.selected != undefined) {
            $scope.queryMap.org__id = $scope.orglist.selected.id;
        } else if ($scope.orglist.selected == undefined) {
            delete $scope.queryMap.org__id;
        }
        if ($scope.modelList.selected != undefined) {
            $scope.queryMap.modelId= $scope.modelList.selected.id;
        } else if ($scope.modelList.selected == undefined) {
            delete $scope.queryMap.modelId;
        }
        $scope.reload(1, $scope.queryMap);
    };

    // 新增、更新
    $scope.write = function(size, current_data, submit_type) {
        $scope.current_data = angular.copy(current_data);
        var modalInstance = $modal.open({
            templateUrl: 'template/bus/vehicle/writeModal.html?v=a675e2ef97',
            controller: 'BusVehicleModalCtrl',
            size: size,
            resolve: {
                param: function() {
                    return {
                        "data": $scope.current_data,
                        "type": submit_type
                    };
                }
            }
        });

        modalInstance.result.then(function(param) {
            if (param.type == 'update') {
                updater.fields = param.data;
                updater.id = param.data.id;
                updater.resource = 'vehicle/bs/update';
                $scope.page.mod(updater, $scope.reload);
            } else if (param.type == 'insert') {
                inserter.fields = param.data;
                inserter.resource = 'vehicle/bs/insert';
                $scope.page.add(inserter, $scope.reload);
            }
        });
    };

    //删除
    $scope.del = function(size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
        modalInstance.result.then(function(param) {
            if (param.type == 'delete') {
                deleter.id = param.data.id;
                deleter.resource = 'vehicle/bs/' + deleter.id + '/delete';
                $scope.page.del(deleter, $scope.reload);
            }
        });
    };

    //获取组织列表
    (function() {
        var promise = ajax.call(new Req.GET({
            resource: 'organization/3/list'
        }));
        promise.then(function(result) {
            $scope.orglist = result.result;
        }, function() { });
    })();
    
    //获取车辆型号列表
    (function(){
        var promise = ajax.call(new Req.GET({
            resource: 'vehicle/bs/model/list'
        }));
        promise.then(function(result) {
            $scope.modelList = result.result;
        }, function() { });
    })();
})
